package com.yangyongchao.resumedownload.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.yangyongchao.resumedownload.entires.ThreadInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 杨永超的电脑 on 2017/9/9.
 * 接口实现
 */

public class ThreadDaoImpl implements ThreadDAO {

    private DBHelper mHelper = null;
    public ThreadDaoImpl(Context context){
        //创建了一个数据访问对象
        mHelper = new DBHelper(context);
    }

    /**
     * 插入
     * @param threadInfo
     */
    @Override
    public void insertThread(ThreadInfo threadInfo) {
        SQLiteDatabase db = mHelper.getWritableDatabase();

        db.execSQL("insert into thread_info(threan_id,url,start,end,finished)values(?,?,?,?,?)",
        new Object[]{threadInfo.getId(),threadInfo.getUrl(),threadInfo.getStart()
                ,threadInfo.getEnd(),threadInfo.getFinished()});

        db.close();
    }

    /**
     * 删除
     * @param url 地址
     * @param thread_id id
     */
    @Override
    public void deleteThread(String url, int thread_id) {

        SQLiteDatabase db = mHelper.getWritableDatabase();
        db.execSQL("delete from thread_info where url = ? and thread_id = ?",
                new Object[]{url,thread_id});
        db.close();
    }

    /**
     * 更新
     * @param url
     * @param thread_id
     * @param finished
     */
    @Override
    public void updateThread(String url, int thread_id, int finished) {
        SQLiteDatabase db = mHelper.getWritableDatabase();
        db.execSQL("update thread_id set thread_info finished = ? where url = ? and thread_id = ?",
                new Object[]{finished,url,thread_id});
        db.close();
    }

    /**
     * 获得进度
     * @param url
     * @return
     */
    @Override
    public List<ThreadInfo> getThread(String url) {
        SQLiteDatabase db = mHelper.getWritableDatabase();
        List<ThreadInfo>list = new ArrayList<>();

        Cursor cursor = db.rawQuery("select * from thread_info where url = ?",new String[]{url});
        while (cursor.moveToNext()){
            ThreadInfo threadInfo = new ThreadInfo();
            threadInfo.setId(cursor.getInt(cursor.getColumnIndex("thread_id")));
            threadInfo.setUrl(cursor.getString(cursor.getColumnIndex("url")));
            threadInfo.setStart(cursor.getInt(cursor.getColumnIndex("start")));
            threadInfo.setEnd(cursor.getInt(cursor.getColumnIndex("end")));
            threadInfo.setFinished(cursor.getInt(cursor.getColumnIndex("finished")));

            list.add(threadInfo);
        }
        cursor.close();
        db.close();
        return list;
    }

    /**
     * 查询是否存在
     * @param url
     * @param thread_id
     * @return
     */
    @Override
    public boolean isExisti(String url, int thread_id) {
        SQLiteDatabase db = mHelper.getWritableDatabase();
        List<ThreadInfo>list = new ArrayList<>();

        Cursor cursor = db.rawQuery("select * from thread_info where url = ? and thread_id = ?" ,new String[]{url, String.valueOf(thread_id)});
        boolean exists = cursor.moveToNext();
        cursor.close();
        db.close();
        return exists;
    }
}
